<?php

class curlp {

	private $data = '';

	private $cookie_file = '';

	private $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";

    private $proxy = '';
    private $proxyport = '';
    private $proxyuser = '';
    private $proxypassword = '';
    
    
    public function get($url, $post_array) {
    
        $post_string = $this->build_post_data($post_array);

        $data = $this->curl_request($url, $post_string);

        preg_match("!<head.*?>(.*?)</head>!s", $data, $output);
        $return['head'] = $output[1];
        
        preg_match("!<body.*?>(.*?)</body>!s", $data, $output);
		$return['body'] = $output[1];

        return $return;
    }

    public function set_user_agent($user_agent) {
        $this->user_agent = $user_agent;
    }

    public function get_user_agent() {
		return $this->user_agent;
	}

    public function set_proxy_settings($proxy, $proxyport, $proxyuser = '', $proxypassword = '') {
        $this->proxy = $proxy;
        $this->proxyport = $proxyport;
        $this->proxyuser = $proxyuser;
        $this->proxypassword = $proxypassword;
    }

    private function get_proxy_settings() {

        $proxy_settings = array();

        $proxy_settings['proxy'] = $this->proxy;
        $proxy_settings['proxyport'] = $this->proxyport;
        $proxy_settings['proxyuser'] = $this->proxyuser;
        $proxy_settings['proxypassword'] = $this->proxypassword;
        
        return $proxy_settings;
    }

    private function build_post_data($postdata_array) {
        $postdata_string = '';
		foreach($postdata_array as $key => $value) {
            if($key != 'url')
                $postdata_string .= ($postdata_string == '' ? '' : '&') . $key . "=" . $value;
		}
        return $postdata_string;
    }

    private function curl_request($target, $postdata = ''){

    	$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $target);
        curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_file);
        if($this->proxy != '' && $this->proxyport != '') {
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
            curl_setopt($ch, CURLOPT_PROXY, $this->proxy.":".$this->proxyport);
            if($this->proxyuser != '')
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->proxyuser.":".$this->proxypassword);
        }
		if($postdata != '') {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($ch, CURLOPT_POST, 1);
        }
		$data = curl_exec($ch);
		curl_close($ch);
    
        return $data;
    }
}

?>